package ua.kpi_java_training.zabrodskyi.module3;

/**
 * The class is intended for count of statistics of operation of the program
 * and its output to the screen.
 * 
 * @author Maksym Zabrodskyi
 * @version 25-Jun-2013
 */
public class Statistics {

	private static int numberRuinedVictims = 0;
	private static int numberSpawnedVictims = 0;
	private static String statisticsOfLife = "";
	private int numberElements;
	private int numberBarriers;
	private int numberPredators;
	private int numberVictims;
	private int lengthLife;
	private double percentOfFullness;
	private Environment environment;

	/**
	 * Creates a new copy of statistics.
	 * 
	 * @param environment, for which will be calculate statistics.
	 * @param settings of environment.
	 */
	public Statistics(Environment environment, Settings settings) {
		this.environment = environment;
		numberBarriers = settings.getNumberBarriers();
		numberPredators = settings.getNumberPredators();
		numberVictims = settings.getNumberVictims();
		numberElements = numberBarriers + numberPredators + numberVictims;
		lengthLife = settings.getLengthLife();
	}

	/**
	 * Adds the message of work of the program to a string of statistics.
	 * 
	 * @param message of work of the program.
	 */
	public static void addMessage(String message) {
		statisticsOfLife = statisticsOfLife + message + "\n";
	}

	/**
	 * Does output of all statistics of work of the program to the
	 * screen by a call of other methods.
	 */
	public void printAllStatistics() {
		addHeaderToStatisticsMessage();
		calculateLastResults();
		addLastResultsToStatisticsMessage();
		printStatisticsMessage();
	}

	/**
	 * Does output to the screen of heading of statistics - the initial data
	 * of work of the program.
	 */
	private void addHeaderToStatisticsMessage() {
		addMessage("* * * * *  LIFE IN ENVIRONMENT  * * * * *");
		addMessage(environment.toString());
		addMessage(String.format("Life goes %s times", lengthLife));
		addMessage(String.format("The first numbers of Elements = %s",
				numberElements));
		addMessage(String.format("- PREDATORS = %s", numberPredators));
		addMessage(String.format("- VICTIMS = %s", numberVictims));
		addMessage(String.format("- Barriers = %s", numberBarriers));
	}

	/**
	 * Does output to the screen of a string of statistics in which all
	 * statistics of work of the program is stored.
	 */
	private void printStatisticsMessage() {
		Printer.printToConsole(statisticsOfLife);
	}

	/**
	 * Does calculation of results of work of the program.
	 */
	private void calculateLastResults() {
		numberElements = environment.getCountAllElements();
		numberPredators = environment.getCountElements(ElementType.PREDATOR);
		numberVictims = environment.getCountElements(ElementType.VICTIM);
		percentOfFullness = numberElements * 100.0 / environment.getSize();
	}

	/**
	 * Adds last results of work of the program to a string of statistics.
	 */
	private void addLastResultsToStatisticsMessage() {
		addMessage("= = =  RESULTS  = = =");
		addMessage(String.format("Numbers of ruined Victims = %s",
				numberRuinedVictims));
		addMessage(String.format("Numbers of spawned Victims = %s",
				numberSpawnedVictims));
		addMessage(String.format("The last numbers of Elements = %s",
				numberElements));
		addMessage(String.format("- PREDATORS = %s", numberPredators));
		addMessage(String.format("- VICTIMS = %s", numberVictims));
		addMessage(String.format("- Barriers = %s", numberBarriers));
		addMessage(String.format("Percent of fullness of Environment = %.1f",
				percentOfFullness));
	}

	/**
	 * Increases quantity of the lost victims.
	 */
	public static void countRuinedVictim() {
		numberRuinedVictims++;
	}

	/**
	 * Increases quantity of the born victims.
	 */
	public static void countSpawnedVictim() {
		numberSpawnedVictims++;
	}

}
